package com.xzakota.minesweeper.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 日志工具类
 *
 * @author Xzakota
 */
public class XLogger {
    public static StackTraceElement findCaller() {
        StackTraceElement[] callStack = Thread.currentThread().getStackTrace();

        StackTraceElement caller = null;
        String logClassName = XLogger.class.getName();
        boolean isEachLogClass = false;

        for (StackTraceElement s : callStack) {
            if (logClassName.equals(s.getClassName())) {
                isEachLogClass = true;
            }
            if (isEachLogClass) {
                if (!logClassName.equals(s.getClassName())) {
                    caller = s;
                    break;
                }
            }
        }

        return caller;
    }

    private static Logger logger() {
        StackTraceElement caller = findCaller();
        if (null == caller) {
            return LoggerFactory.getLogger(XLogger.class);
        }

        return LoggerFactory.getLogger(caller.getClassName() + "." + caller.getMethodName() + "() Line: " + caller.getLineNumber());
    }

    public static void trace(String msg) {
        trace(msg, null);
    }

    public static void trace(String msg, Throwable e) {
        logger().trace(msg, e);
    }

    public static void debug(String msg) {
        debug(msg, null);
    }

    public static void debug(String msg, Throwable e) {
        logger().debug(msg, e);
    }

    public static void info(String msg) {
        info(msg, null);
    }

    public static void info(String msg, Throwable e) {
        logger().info(msg, e);
    }

    public static void warn(String msg) {
        warn(msg, null);
    }

    public static void warn(String msg, Throwable e) {
        logger().warn(msg, e);
    }

    public static void warn(Throwable e) {
        warn(e.getMessage(), e);
    }

    public static void error(String msg) {
        error(msg, null);
    }

    public static void error(Throwable e) {
        error(e.getMessage(), e);
    }

    public static void error(String msg, Throwable e) {
        logger().error(msg, e);
    }
}